package org.minuframework.file.upload;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.minuframework.core.ApplicationConfigurer;
import org.minuframework.exception.ExceptionFailed;

import com.google.common.collect.Lists;

/**
 * 文件上传
 * @author 米mi
 */
@Deprecated
public final class FileUpload extends DefaultFileUpload{
    
    public FileUpload(FileParamerConfigurer fileParamerConfigurer, FileTypesConfigurer fileTypesConfigurer, String charset){
        super(fileParamerConfigurer, fileTypesConfigurer, charset);
    }
    
    // ------------------------------------------------------------------------
    
    // ------------------------------------------------------------------------
    
    public static String upload(HttpServletRequest request, FileUploadType type){
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUpload(request, type);
    }
    
    public static List<String> uploads(HttpServletRequest request, FileUploadType type){
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUploadList(request, type);
    }
    
    // ------------------------------------------------------------------------
    
    // ------------------------------------------------------------------------
    
    public static String upload(FileItem item, FileUploadType type) throws ExceptionFailed{
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUpload(item, type);
    }
    
    public static String uploadSafe(FileItem item, FileUploadType type){
        try {
            return upload(item, type);
        } catch (ExceptionFailed e) {}
        
        return null;
    }
    
    public static List<String> uploads(List<FileItem> items, FileUploadType type) throws ExceptionFailed{
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUpload(items, type);
    }
    
    public static List<String> uploadsSafe(List<FileItem> items, FileUploadType type){
        try {
            return uploads(items, type);
        } catch (ExceptionFailed e) {}
        
        return null;
    }
    
    // ------------------------------------------------------------------------
    
    // ------------------------------------------------------------------------
    
    public static String uploadBase64(byte[] imgstr, FileUploadType type) throws ExceptionFailed{
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUploadBase64(imgstr, type);
    }
    
    public static String uploadBase64Safe(byte[] imgstr, FileUploadType type){
        try {
            return uploadBase64(imgstr, type);
        } catch (ExceptionFailed e) {
            return null;
        }
    }
    
    public static String uploadBase64(byte[] imgstr, String type) throws ExceptionFailed{
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUploadBase64(imgstr, type);
    }
    
    public static String uploadBase64Safe(byte[] imgstr, String type){
        try {
            return uploadBase64(imgstr, type);
        } catch (ExceptionFailed e) {
            return null;
        }
    }
    
    public static String uploadBase64(String imgstr, FileUploadType type) throws ExceptionFailed{
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUploadBase64(imgstr, type);
    }
    
    public static String uploadBase64Safe(String imgstr, FileUploadType type){
        try {
            return uploadBase64(imgstr, type);
        } catch (ExceptionFailed e) {
            return null;
        }
    }
    
    public static String uploadBase64(String imgstr, String type) throws ExceptionFailed{
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUploadBase64(imgstr, type);
    }
    
    public static String uploadBase64Safe(String imgstr, String type){
        try {
            return uploadBase64(imgstr, type);
        } catch (ExceptionFailed e) {
            return null;
        }
    }
    
    public static List<String> uploadsBase64(List<String> imgstr, FileUploadType type) throws ExceptionFailed{
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUploadBase64(imgstr, type);
    }
    
    public static List<String> uploadsBase64Safe(List<String> imgstrs, FileUploadType type){
        try {
            return uploadsBase64(imgstrs, type);
        } catch (ExceptionFailed e) {
            return Lists.newArrayList();
        }
    }
    
    public static List<String> uploadsBase64(List<String> imgstr, String type) throws ExceptionFailed{
        return new FileUploadBuilder().setCharset(ApplicationConfigurer.DEFAULE_MINU_CHARSET).create().fileUploadBase64(imgstr, type);
    }
    
    public static List<String> uploadsBase64Safe(List<String> imgstrs, String type){
        try {
            return uploadsBase64(imgstrs, type);
        } catch (ExceptionFailed e) {
            return Lists.newArrayList();
        }
    }
    
}
